package com.vtquang.mymaps;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.vtquang.mymaps.adapter.PlaceLocationAdapter;

public class Place_Location_Activity extends Activity implements
		OnItemClickListener, OnClickListener {

	public static final String LONGTITUE_KEY = "lontitue"; 
	public static final String LATITUE_KEY = "latitue";
	public static final String SEARCHLOCATION_KEY = "search_location";
	
	private ListView lv_place;
	private String keyWord;
	private PlaceLocationAdapter adapter;
	private List<MyLocation> listLocation;
	private int distance;
	private Double lon;
	private Double lat;
	private ProgressDialog progressDialog;
	private Button btnViewMap;
	private Button btnBack;

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				listLocation = (List<MyLocation>) (msg.obj);
				sortListLocation(listLocation);
				adapter = new PlaceLocationAdapter(getApplicationContext(),
						listLocation);
				lv_place.setAdapter(adapter);
				progressDialog.dismiss();
				btnViewMap.setVisibility(View.VISIBLE);
				btnBack.setVisibility(View.VISIBLE);
				break; 
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_place_location);

		btnViewMap = (Button)findViewById(R.id.btnViewmap);
		btnBack = (Button)findViewById(R.id.btnback);
		lv_place = (ListView) findViewById(R.id.lv_place_location);
		lv_place.setOnItemClickListener(this);

		btnViewMap.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		keyWord = bundle.getString(GetPlace_Activity.PLACE_CHOSE_KEY);
		lon = bundle.getDouble(MainActivity.MYLOCATION_LON_KEY);
		lat = bundle.getDouble(MainActivity.MYLOCATION_LAT_KEY);
		distance = bundle.getInt(GetPlace_Activity.DISTANCE_KEY);
		
		progressDialog = new ProgressDialog(this);  
		progressDialog.setMessage("Please wait....");
		progressDialog.show();
	
		Thread thread = new Thread(new GetPlaceLoaction());
		thread.start();
	}

	class GetPlaceLoaction implements Runnable {
		@Override
		public void run() {
			List<MyLocation> l = WebProvider.getLocation(keyWord,
					lat.toString(), lon.toString(), distance);
			Message msg = myHandler.obtainMessage(1, l);
			myHandler.sendMessage(msg);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		MyLocation location = listLocation.get(position);
		
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putDouble(LONGTITUE_KEY, Double.parseDouble(location.get_lng()));
		bundle.putDouble(LATITUE_KEY, Double.parseDouble(location.get_lat()));
		intent.putExtras(bundle);
		
		setResult(RESULT_OK, intent);
		finish();
	}

	private void sortListLocation(List<MyLocation> list){
		int count = list.size();
		for (int i = 0; i < count - 1; i++) {
			MyLocation iloc = list.get(i);
			for (int j = i + 1; j < count; j++) {
				MyLocation jloc = list.get(j);
				if(iloc.get_distance() > jloc.get_distance()){
					list.remove(j);
					list.remove(i); 
					
					list.add(i, jloc);
					list.add(j, iloc);  
					
					iloc = jloc;
				}
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnViewmap:
			Intent intent = new Intent();
			ArrayList<SearchLocation> list = new ArrayList<SearchLocation>();
			for (MyLocation loc : listLocation) {
				SearchLocation sloc = new SearchLocation();
				sloc.set_lat(Double.parseDouble(loc.get_lat()));
				sloc.set_lng(Double.parseDouble(loc.get_lng()));
				list.add(sloc);
			}

			Bundle bundle = new Bundle();
			bundle.putParcelableArrayList(SEARCHLOCATION_KEY, list);
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish(); 
			break;
		case R.id.btnback:
			setResult(RESULT_CANCELED);
			finish();
			break; 
		default:
			break;
		}
	}
}
